// WordAdapter.kt
package com.example.sunchaodong

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class WordAdapter(private val words: MutableList<String>, private val listener: OnItemLongClickListener) :
    RecyclerView.Adapter<WordAdapter.WordViewHolder>() {

    interface OnItemLongClickListener {
        fun onItemLongClick(position: Int, view: View): Boolean
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WordViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false)
        return WordViewHolder(view)
    }

    override fun onBindViewHolder(holder: WordViewHolder, position: Int) {
        holder.textView.text = words[position]
        holder.itemView.setOnLongClickListener {
            listener.onItemLongClick(position, it)
        }
    }

    override fun getItemCount(): Int = words.size

    class WordViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(android.R.id.text1)
    }
}